package cn.iocoder.yudao.module.blog.controller.admin.improve;

import cn.iocoder.yudao.module.blog.dal.dataobject.improvechapter.BlogImproveChapterDO;
import cn.iocoder.yudao.module.blog.dal.dataobject.improvesection.BlogImproveSectionDO;
import cn.iocoder.yudao.module.blog.service.improvechapter.BlogImproveChapterService;
import cn.iocoder.yudao.module.blog.service.improvesection.BlogImproveSectionService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.improve.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.improve.BlogImproveDO;
import cn.iocoder.yudao.module.blog.convert.improve.BlogImproveConvert;
import cn.iocoder.yudao.module.blog.service.improve.BlogImproveService;

@Tag(name = "管理后台 - 课程")
@RestController
@RequestMapping("/blog/improve")
@Validated
public class BlogImproveController {

    @Resource
    private BlogImproveService improveService;

    @Resource
    private BlogImproveSectionService improveSectionService;

    @Resource
    private BlogImproveChapterService improveChapterService;

    @PostMapping("/create")
    @Operation(summary = "创建课程")
    @PreAuthorize("@ss.hasPermission('blog:improve:create')")
    public CommonResult<Long> createImprove(@Valid @RequestBody BlogImproveCreateReqVO createReqVO) {
        return success(improveService.createImprove(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程")
    @PreAuthorize("@ss.hasPermission('blog:improve:update')")
    public CommonResult<Boolean> updateImprove(@Valid @RequestBody BlogImproveUpdateReqVO updateReqVO) {
        improveService.updateImprove(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课程")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:improve:delete')")
    public CommonResult<Boolean> deleteImprove(@RequestParam("id") Long id) {
        improveService.deleteImprove(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课程")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:improve:query')")
    public CommonResult<BlogImproveRespVO> getImprove(@RequestParam("id") Long id) {
        BlogImproveDO improve = improveService.getImprove(id);
        List<BlogImproveChapterDO> chapterDOList = improveChapterService.getImproveChapterByImproveId(id);
        return success(BlogImproveConvert.INSTANCE.convert(improve));
    }

    @GetMapping("/list")
    @Operation(summary = "获得课程列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:improve:query')")
    public CommonResult<List<BlogImproveRespVO>> getImproveList(@RequestParam("ids") Collection<Long> ids) {
        List<BlogImproveDO> list = improveService.getImproveList(ids);

        return success(BlogImproveConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程分页")
    @PreAuthorize("@ss.hasPermission('blog:improve:query')")
    public CommonResult<PageResult<BlogImproveRespVO>> getImprovePage(@Valid BlogImprovePageReqVO pageVO) {
        PageResult<BlogImproveDO> pageResult = improveService.getImprovePage(pageVO);

        List<Long> improveIds = new ArrayList<>();
        for(int i = 0; i<pageResult.getList().size(); i++){
            improveIds.add(pageResult.getList().get(i).getId());
        }

        List<BlogImproveChapterDO> chapterDOList = improveChapterService.getImproveChapterByImproveIds(improveIds);

        List<BlogImproveSectionDO> sectionDOList = improveSectionService.getImproveSectionByImproveIds(improveIds);

        return success(BlogImproveConvert.INSTANCE.convertPage(pageResult, chapterDOList, sectionDOList));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程 Excel")
    @PreAuthorize("@ss.hasPermission('blog:improve:export')")
    @OperateLog(type = EXPORT)
    public void exportImproveExcel(@Valid BlogImproveExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlogImproveDO> list = improveService.getImproveList(exportReqVO);
        // 导出 Excel
        List<BlogImproveExcelVO> datas = BlogImproveConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "课程.xls", "数据", BlogImproveExcelVO.class, datas);
    }

}
